
from django.contrib import admin

from .models import (
    Spr_dlj,
    Spr_lpu,
    Spr_nowork,
    Spr_opl,
    Spr_otd,
    Spr_sdl,
    Spr_smena,
    Spr_worker,
    Spr_worker_dlj,
    worker_grf,
    worker_grfn,
    worker_grfo,
)

# class InlineImage(admin.TabularInline):
#     model = Images


@admin.register(Spr_lpu)
class Spr_lpuAdmin(admin.ModelAdmin):
    list_display = ("id", "ter", "lpu", "name_lpu", "adress", "dred", "is_active")
    list_display_links = ("name_lpu",)
    search_fields = ("name_lpu",)
    readonly_fields = ("dred",)
    list_per_page = 25


@admin.register(Spr_otd)
class Spr_otdAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "otd",
        "name_otd",
        "name_otdkr",
        "type_otd",
        "lpu",
        "dred",
        "is_active",
    )  # список полей которые отображаются
    list_display_links = ("name_otd",)  # список полей для перехода в редактирование данных
    search_fields = ("name_otd",)  # поля по которым идет поиск
    list_filter = ("lpu",)  # поля для фильтрации
    list_per_page = 25  # количество данных на одной странице


@admin.register(Spr_sdl)
class Spr_sdlAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "sdl",
        "name_sdl",
    )
    list_display_links = (
        "id",
        "sdl",
        "name_sdl",
    )
    search_fields = ("name_sdl",)
    list_filter = ("name_sdl",)
    list_per_page = 25


@admin.register(Spr_opl)
class Spr_oplAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "opl",
        "name_opl",
    )
    list_display_links = ("name_opl",)
    search_fields = ("name_opl",)
    list_per_page = 25


@admin.register(Spr_dlj)
class Spr_dljAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "dlj",
        "name_dlj",
        "dred",
        "is_active",
    )
    list_display_links = (
        "id",
        "name_dlj",
    )
    search_fields = ("name_dlj",)
    # list_filter = ("name_dlj",)
    list_per_page = 25


@admin.register(Spr_nowork)
class Spr_noworkAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "name_nowork",
    )
    list_display_links = (
        "id",
        "name_nowork",
    )
    search_fields = ("name_nowork",)
    # list_filter = ("name_nowork",)
    list_per_page = 25


@admin.register(Spr_smena)
class Spr_smenaAdmin(admin.ModelAdmin):
    list_display = ("id", "name_smena", "dred", "is_active")
    list_display_links = (
        "id",
        "name_smena",
    )
    search_fields = ("name_smena",)
    list_filter = ("name_smena",)
    list_per_page = 25


@admin.register(Spr_worker)
class Spr_workerAdmin(admin.ModelAdmin):
    list_display = ("id", "mpp", "fam", "im", "ot", "diplom", "snils", "dred", "is_active")
    list_display_links = ("id", "fam", "im", "ot", "is_active")
    search_fields = (
        "fam",
        "im",
        "ot",
    )

    list_per_page = 25


@admin.register(Spr_worker_dlj)
class Spr_worker_dljAdmin(admin.ModelAdmin):
    list_display_links = (
        "id_workerr",
        "full_name",
    )
    list_display = (
        "id",
        "id_workerr",
        "full_name",
        "doc",
        "id_dlj",
        "nlpu",
        "id_otd",
        "id_sdl",
        "work_tip",
        "stavka",
        "dred",
        "is_active",
    )

    list_filter = ("id_lpu",)

    search_fields = [
        "full_name",
    ]
    # raw_id_fields = [
    #     "patient",
    #     "id_last",
    #     "sdl_set",
    #     "user_etap_link",
    # ]

    # readonly_fields = ("dou", "idkv", "nkv")
    list_per_page = 25

    # actions = [
    #     "deClose_nar",
    #     "moveDateClose_nar_12",
    #     "moveDateClose_nar",
    #     "moveDateClose_nar_05",
    #     "moveDateClose_nar_06",
    #     "moveDateClose_nar_07_lgot",
    #     "dePdzr_nar",
    #     "deZav_nar",
    #     "pdzrCheckZav",
    # ]


@admin.register(worker_grfo)
class worker_grfoAdmin(admin.ModelAdmin):
    list_display = ("id", "work_y", "work_m", "id_lpu", "id_otd", "id_opl", "dred", "user_edit")
    list_display_links = ("id",)
    search_fields = (
        "work_y",
        "work_m",
        "id_lpu",
    )
    list_filter = (
        "work_y",
        "work_m",
    )
    list_per_page = 25


@admin.register(worker_grfn)
class worker_grfnAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "id_worker_grfo",
        "id_worker",
        "id_dlj",
        "work_tip",
        "ntime",
        "kdays",
        "smena",
        "dred",
        "user_edit",
    )
    list_display_links = ("id", "id_worker_grfo")
    search_fields = ("id_worker_grfo",)
    list_filter = ("id_worker_grfo",)
    list_per_page = 25


@admin.register(worker_grf)
class worker_grfAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "id_worker_grfo",
        "id_worker_grfn",
        "id_dlj",
        "work_y",
        "work_m",
        "work_d",
        "work_date",
        "range_times",
        "kol_times",
        "id_nowork",
        "dred",
        "user_edit",
    )
    list_display_links = ("id",)
    search_fields = (
        "work_y",
        "work_m",
    )
    list_filter = (
        "work_y",
        "work_m",
    )
    list_per_page = 25


#     @admin.action(description="Открыть наряд в Лаб-ии и Арене")
#     def deClose_nar(self, request, queryset):
#         if queryset.count() > 1:
#             idkv = queryset.values_list("idkv", flat=True)[0]
#         else:
#             idkv = queryset.values_list("idkv", flat=True)[0]
#         print(idkv)
#         try:
#             db.write(f"update pl_uslk set dzr = NULL where idkv='{idkv}'")
#             queryset.update(dzr=None)
#             messages.success(request, "Успешно открыт наряд в Лаб-ии и Арене")
#         except Exception as errexp:
#             messages.error(request, errexp)

#     @admin.action(description="Убрать ПДЗР, снять отметки")
#     def dePdzr_nar(self, request, queryset):
#         if queryset.count() > 1:
#             idkv = queryset.values_list("idkv", flat=True)[0]
#         else:
#             idkv = queryset.values_list("idkv", flat=True)[0]
#         print(idkv)
#         try:
#             queryset.update(pdzr=None, check_zav=False, check_tech=False)
#             db.write(f"update pl_uslk set dzr=null, status=2 where idkv='{idkv}'")
#         except Exception as errexp:
#             messages.error(request, errexp)
#         messages.success(request, "Успешно снята дата ПДЗР и отметки Зав. и Тех., В арене статус 2 дзр нул")

#     @admin.action(description="Убрать Зав")
#     def deZav_nar(self, request, queryset):
#         if queryset.count() > 1:
#             idkv = queryset.values_list("idkv", flat=True)[0]
#         else:
#             idkv = queryset.values_list("idkv", flat=True)[0]
#         print(idkv)
#         try:
#             queryset.update(check_zav=False)
#             messages.success(request, "Отметка Зав установлена False")
#         except Exception as errexp:
#             messages.error(request, errexp)

#     @admin.action(description="ПДЗР на 20.09.2023, Отм.Зав.")
#     def pdzrCheckZav(self, request, queryset):
#         if queryset.count() > 1:
#             idkv = queryset.values_list("idkv", flat=True)[0]
#             stu = queryset.values_list("stu", flat=True)[0]
#         else:
#             idkv = queryset.values_list("idkv", flat=True)[0]
#             stu = queryset.values_list("stu", flat=True)[0]
#         try:
#             sum = db.select(f"select stu from pl_uslk where idkv={idkv}")[0][0]
#             if sum == stu:
#                 dzr = datetime(2023, 9, 20, 00, 00)
#                 print("dzr ", dzr, " idkv ", idkv)
#                 print("summ equal", sum)
#                 db.write(f"update pl_uslk set dzr='{dzr}' where idkv='{idkv}'")
#                 queryset.update(pdzr=dzr, check_zav=True)
#                 messages.success(request, "Успешно обновлена информация по наряду")
#         except Exception as err:
#             print(err)
#             messages.error(request, f"Суммы в Арене {sum} и Гелиос {stu} не совпадают")

#     @admin.action(description="ДЗР на 25.04.2023")
#     def moveDateClose_nar(self, request, queryset):
#         if queryset.count() > 1:
#             idkv = queryset.values_list("idkv", flat=True)[0]
#             stu = queryset.values_list("stu", flat=True)[0]
#         else:
#             idkv = queryset.values_list("idkv", flat=True)[0]
#             stu = queryset.values_list("stu", flat=True)[0]
#         try:
#             sum = db.select(f"select stu from pl_uslk where idkv={idkv}")[0][0]
#             if sum == stu:
#                 dzr = datetime(2023, 4, 25, 00, 00)
#                 print("dzr ", dzr, " idkv ", idkv)
#                 print("summ equal", sum)
#                 db.write(f"update pl_uslk set dzr='{dzr}' where idkv='{idkv}'")
#                 queryset.update(dzr=dzr)
#                 messages.success(request, "Успешно обновлена информация по наряду")
#         except Exception as err:
#             print(err)
#             messages.error(request, f"Суммы в Арене {sum} и Гелиос {stu} не совпадают")

#     @admin.action(description="ДЗР на 25.05.2023")
#     def moveDateClose_nar_05(self, request, queryset):
#         if queryset.count() > 1:
#             idkv = queryset.values_list("idkv", flat=True)[0]
#             stu = queryset.values_list("stu", flat=True)[0]
#         else:
#             idkv = queryset.value("idkv", flat=True)
#             stu = queryset.values("stu", flat=True)
#         try:
#             sum = db.select(f"select stu from pl_uslk where idkv={idkv}")[0][0]
#             if sum == stu:
#                 dzr = datetime(2023, 5, 25, 00, 00)
#                 print("dzr ", dzr, " idkv ", idkv)
#                 print("summ equal", sum)
#                 db.write(f"update pl_uslk set dzr='{dzr}' where idkv='{idkv}'")
#                 queryset.update(dzr=dzr)
#                 messages.success(request, "Успешно обновлена информация по наряду")
#         except Exception as err:
#             print(err)
#             messages.error(request, f"Суммы в Арене {sum} и Гелиос {stu} не совпадают")

#     @admin.action(description="ДЗР на 20.07.2023")
#     def moveDateClose_nar_07_lgot(self, request, queryset):
#         if queryset.count() > 1:
#             idkv = queryset.values_list("idkv", flat=True)[0]
#             stu = queryset.values_list("stu", flat=True)[0]
#         else:
#             idkv = queryset.value("idkv", flat=True)
#             stu = queryset.values("stu", flat=True)
#         try:
#             sum = db.select(f"select stu from pl_uslk where idkv={idkv}")[0][0]
#             if sum == stu:
#                 dzr = datetime(2023, 7, 20, 00, 00)
#                 print("dzr ", dzr, " idkv ", idkv)
#                 print("summ equal", sum)
#                 db.write(f"update pl_uslk set dzr='{dzr}' where idkv='{idkv}'")
#                 queryset.update(dzr=dzr)
#                 messages.success(request, "Успешно обновлена информация по наряду")
#         except Exception as err:
#             print(err)
#             messages.error(request, f"Суммы в Арене {sum} и Гелиос {stu} не совпадают")

#     @admin.action(description="ДЗР на 20.06.2023")
#     def moveDateClose_nar_06(self, request, queryset):
#         if queryset.count() > 1:
#             idkv = queryset.values_list("idkv", flat=True)[0]
#             stu = queryset.values_list("stu", flat=True)[0]
#         else:
#             idkv = queryset.value("idkv", flat=True)
#             stu = queryset.values("stu", flat=True)
#         try:
#             sum = db.select(f"select stu from pl_uslk where idkv={idkv}")[0][0]
#             if sum == stu:
#                 dzr = datetime(2023, 6, 20, 00, 00)
#                 print("dzr ", dzr, " idkv ", idkv)
#                 print("summ equal", sum)
#                 db.write(f"update pl_uslk set dzr='{dzr}' where idkv='{idkv}'")
#                 queryset.update(dzr=dzr)
#                 messages.success(request, "Успешно обновлена информация по наряду")
#         except Exception as err:
#             print(err)
#             messages.error(request, f"Суммы в Арене {sum} и Гелиос {stu} не совпадают")

#     @admin.action(description="ДЗР на 31.12.2022")
#     def moveDateClose_nar_12(self, request, queryset):
#         if queryset.count() > 1:
#             idkv = queryset.values_list("idkv", flat=True)[0]
#             stu = queryset.values_list("stu", flat=True)[0]
#         else:
#             idkv = queryset.value("idkv", flat=True)
#             stu = queryset.values("stu", flat=True)
#         try:
#             sum = db.select(f"select stu from pl_uslk where idkv={idkv}")[0][0]
#             if sum == stu:
#                 dzr = datetime(2022, 12, 31, 00, 00)
#                 print("dzr ", dzr, " idkv ", idkv)
#                 print("summ equal", sum)
#                 db.write(f"update pl_uslk set dzr='{dzr}' where idkv='{idkv}'")
#                 queryset.update(dzr=dzr)
#                 messages.success(request, "Успешно обновлена информация по наряду")
#         except Exception as err:
#             print(err)
#             messages.error(request, f"Суммы в Арене {sum} и Гелиос {stu} не совпадают")

# admin.site.site_header = "Администрирование Helios"


# @admin.register(User_sdl)
# class User_sdlAdmin(admin.ModelAdmin):
#     list_display = ("id", "fio", "user", "fam", "dolzh", "lpu")
#     filter_horizontal = (
#         "sdls",
#         "access",
#     )
#     list_display_links = ("id", "user")

#     search_fields = ["user__fam", "user__login", "sdls__name_sdl", "lpu__name_lpu"]
#     list_filter = ("user",)
#     list_per_page = 25

#     actions = ["to_pol5", "to_pol_kedr", "to_pol4", "to_pol3", "to_pol2", "to_pol1", "to_pol_topki"]

#     @admin.action(description="Перевод в Пол5")
#     def to_pol5(self, request, queryset):
#         queryset.update(lpu=7)

#     @admin.action(description="Перевод в Кедровку")
#     def to_pol_kedr(self, request, queryset):
#         queryset.update(lpu=6)
